package de.hska.master.algo;

import java.io.FileWriter;
import java.io.IOException;

import de.hska.master.model.Model;
import de.hska.master.model.Point;
import de.hska.master.model.PointsInACircleModel;
import de.hska.master.model.RandomModel;

public class TimeMeasurementFileWriter {

	/**
	 * Executes the two scenarios\n 1. measure performance of random points 2.
	 * measure performance of random points on a circle
	 * 
	 * @throws InterruptedException
	 * @throws IOException
	 */
	public void writeFilesWithTimeMeasurement() throws InterruptedException,
			IOException {
		write("DokuAlgoLab/Zeitmessungen/randomPointsTimeMeasurementGrahamScan.csv", true,
				Algo.GRAHAMSCAN);
		write("DokuAlgoLab/Zeitmessungen/pointsInACircleTimeMeasurementGrahamScan.csv", false,
				Algo.GRAHAMSCAN);

		write("DokuAlgoLab/Zeitmessungen/randomPointsTimeMeasurementMergeHull.csv", true, Algo.MERGEHULL);
		write("DokuAlgoLab/Zeitmessungen/pointsInACircleTimeMeasurementMergeHull.csv", false, Algo.MERGEHULL);
		
		write("DokuAlgoLab/Zeitmessungen/randomPointsTimeMeasurementQuickHull.csv", true, Algo.QUICKHULL);
		write("DokuAlgoLab/Zeitmessungen/pointsInACircleTimeMeasurementQuickHull.csv", false, Algo.QUICKHULL);
	}

	private void write(String filename, boolean isRandom, Algo algorithm)
			throws IOException, InterruptedException {
		FileWriter writer = new FileWriter(filename);

		writer.append("AnzahlPunkte");
		writer.append('	');
		writer.append("Zeit");
		writer.append('\n');

		int numberOfElements = 250 / 2;

		for (int i = 1; i <= 16; i++) {
			numberOfElements = 2 * numberOfElements;

			Model model = null;
			if (isRandom)
				model = new RandomModel(numberOfElements);
			else
				model = new PointsInACircleModel(numberOfElements,
						numberOfElements * 10, new Point(0, 0));

			TimeMeasurement timeMeasurement = new TimeMeasurement(model);
			if (algorithm.equals(Algo.GRAHAMSCAN))
				timeMeasurement.execAndMeasureAlgorithmGrahamScan();
			if (algorithm.equals(Algo.MERGEHULL))
				timeMeasurement.execAndMeasureAlgorithmMergeHull();
			if (algorithm.equals(Algo.QUICKHULL))
				timeMeasurement.execAndMeasureAlgorithmQuickHull();

			double time = timeMeasurement.getDuration();

			writer.append(numberOfElements + "");
			writer.append('	');
			writer.append((time) + "");
			writer.append('\n');

		}

		writer.flush();
		writer.close();
	}

	public static void main(String[] args) throws InterruptedException,
			IOException {
		System.out.print("Starting time measurement... ");
		new TimeMeasurementFileWriter().writeFilesWithTimeMeasurement();
		System.out.println("done");
	}

	private enum Algo {
		GRAHAMSCAN, MERGEHULL, QUICKHULL
	}

}
